<?php
/**
 * The model for grid filters
 *
 *
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @author     Jipa Ciprian <ciprian.jipa@yahoo.com>
 * @copyright  Copyright (c) 2012
 * @version    $Id: User.php 43 2012-09-27 07:14:48Z mcalin45@gmail.com $
 */

namespace Crc\Grid;

use Doctrine\ORM\QueryBuilder,
    Doctrine\Common\Collections AS Collections;

/**
 * Class for filters.
 *
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @author     Jipa Ciprian <ciprian.jipa@yahoo.com>
 * @copyright  Copyright (c) 2012
 */

abstract class Filter
{
    /**
     * Set true if filter is selected
     *
     * @var boolean
     */
    protected $_selected = false;

    /**
     * Filter label
     *
     * @var string
     */
    protected $_label;


    /**
     * Filter value
     *
     * @var mixed
     */
    protected $_value;


    /**
     * Selected operator
     *
     * @var \Crc\Grid\Operator
     * @see \Crc\Grid\Operator
     */
    protected $_operator;


    /**
     * Filter operators
     *
     * @var Collection of \Crc\Grid\Operator
     * @see \Crc\Grid\Operator
     */
    protected $_operators = NULL;


    /**
     * Field on which the filter is applied
     * @var string
     */
    protected $_field;

    /**
     * Class construct
     * @param string $field
     */
    public function __construct($field)
    {
        $this->_field = $field;
    }


    public function getField()
    {
        return $this->_field;
    }

    public function setLabel($label)
    {
        $this->_label = $label;
        return $this;
    }

    public function getLabel()
    {
        return $this->_label;
    }

    public function setSelected($selected = true)
    {
        $this->_selected = $selected;
        return $this;
    }


    /**
     * Return $_selected value
     *
     * @return boolean
     */
    public function isSelected()
    {
        return $this->_selected;
    }

    public function setValue($value)
    {
        $this->_value = $value;
        return $this;
    }

    public function getValue()
    {
        return $this->_value;
    }


    /**
     * Set selected operator
     *
     * @see \Crc\Grid\Operator
     * @param \Crc\Grid\Operator
     * @return \Crc\Grid\Filter
     */
    public function setOperator(\Crc\Grid\Operator $operator)
    {
        $this->_operator = $operator;
        return $this;
    }

    /**
     * Get selected operator
     *
     * @see \Crc\Grid\Operator
     * @return \Crc\Grid\Operator
     */

    public function getOperator()
    {
        return $this->_operator;
    }



    /**
     * Add where clause coresponding to filter
     *
     * @param  Doctrine\DBAL\Query\QueryBuilder $queryBuilder
     * @see Doctrine\DBAL\Query\QueryBuilder
     * @return Doctrine\DBAL\Query\QueryBuilder
     */
    public function processFilter(QueryBuilder $queryBuilder)
    {
        $operator = $this->getOperator();
        if (!($operator instanceof \Crc\Grid\Operator)) {
            throw new \Exception("Invalid operator in filter " . get_class($this));
        }

        return $operator->processOperator($queryBuilder);
    }


    /**
     * Get filter operators
     *
     * @return \Crc\Grid\Operator Collection
     */
    public function getOperators()
    {
        if ($this->_operators === NULL) {
            $this->_operators = new Collections\ArrayCollection();
            foreach($this->_operatorsText as $operatorText) {
                $operatorClass = get_class($this) . '\\' . $operatorText;
                $operator = new $operatorClass();
                $this->_operators->add($operator);
            }
        }

        return $this->_operators;
    }

    /**
     * Find an operator by name
     * @param string  $name
     * @return  Crc\Grid\Operator
     */
    public function getOperatorByName($name)
    {
        if (trim($name) == "") {
            return false;
        }

        foreach ($this->getOperators() as $operator) {
            if ($operator->getName() == $name) {
                return $operator;
            }
        }
        return false;
    }

}